﻿
using UnityEngine;

public class GameController: MonoBehaviour
{
    public static GameController Instance;

    public GameStatus GameStatus;
    
    private void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        //TODO 事件系统
        /*GameEntry.EventSystem.Subscribe(PauseEventArgs.Name, OnPauseHandler);
        GameEntry.EventSystem.Subscribe(GameOverEventArgs.Name, OnGameOverHandler);
        GameEntry.EventSystem.Subscribe(GameRestartEventArgs.Name, OnGameRestartHandler);
        GameEntry.EventSystem.Subscribe(ReturnHomeEventArgs.Name, OnReturnHomeHandler);*/
    }

    /*private void OnReturnHomeHandler(object sender,GameEventArgs e)
    {
        GameEntry.BoUI.Close(UIType.UIPause);
        GameEntry.BoUI.CreateUI<UIHomeComponent>(UIType.UIHome);
        Time.timeScale = 0f;
        this.GameStatus = GameStatus.STOP;
    }

    private void OnGameRestartHandler(object sender, GameEventArgs e)
    {
        GameEntry.BoUI.Close(UIType.UIPause);
        Debug.Log("-------------->on game restart handler");
        Time.timeScale = 1f;
        this.RestartGame();
    }

    private void OnGameOverHandler(object sender, GameEventArgs e)
    {
        if (GameStatus == GameStatus.RUNNING)
        {
            Time.timeScale = 0f;
            GameStatus = GameStatus.STOP;
            GameEntry.BoUI.CreateUI<UIReviveComponent>(UIType.UIRevive);
        }
    }*/

    /*private void OnPauseHandler(object sender, GameEventArgs e)
    {
        PauseEventArgs args = (PauseEventArgs) e;
        if (args.PauseStatus)
        {
            this.GameStatus = GameStatus.PAUSE;
            Time.timeScale = 0f;
            GameEntry.BoUI.CreateUI<UIPauseComponent>(UIType.UIPause);
            GameManager.Instance.canFire = false;
        }
        else
        {
         
            Time.timeScale = 1f;
            GameEntry.BoUI.Close(UIType.UIPause);
            this.GameStatus = GameStatus.RUNNING;
        }
    }*/

    public void StartGame()
    {
//        GameEntry.Sound.PlayMusic("bgm", 0, 1, true, false);

        GameStatus = GameStatus.RUNNING;
        string bundleName = "gameresources.unity3d";
//        GameEntry.Res.LoadBundle(bundleName);
        GameObject prefab = Bingo.Facades.Resources.LoadSync<GameObject>( "GameBoard",bundleName);// (GameObject) GameEntry.Res.GetAsset(bundleName, "GameBoard");
        GameObject board = UnityEngine.Object.Instantiate(prefab);
        board.transform.SetParent(this.transform);    
        board.transform.localScale = Vector3.one;
        board.transform.localPosition = Vector3.zero;
    }


    public void RestartGame()
    {
        GameManager.Instance.RestartGame();
        GameStatus = GameStatus.RUNNING;
        /*GameEntry.Sound.Stop("bgm");
        GameEntry.Sound.PlayMusic("bgm", 0, 1, true, false);*/
    }
}

public enum GameStatus
{
    STOP,
    RUNNING,
    PAUSE
}